package com.qdsgjsfk.vision.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.eventbus.Logout;
import com.qdsgjsfk.vision.ui.dialog.LogoutDialog;
import com.qdsgjsfk.vision.util.NetUtil;
import com.qdsgjsfk.vision.util.PackageUtils;
import com.qdsgjsfk.vision.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.client.RestClient;
import com.rest.response.UserInfoResponse;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import util.PreferencesHelper;

/**
 * Created by HP on 2019/1/3.
 */

public class UserActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_account)
    TextView tv_account;
    @BindView(R.id.btn_confirm)
    TextView btn_confirm;
    @BindView(R.id.tv_school)
    TextView tv_school;
    @BindView(R.id.tv_version)
    TextView tv_version;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.img_person)
    ImageView imgPerson;
    @BindView(R.id.btn_userinfo)
    RelativeLayout btnUserinfo;
    @BindView(R.id.img_password)
    ImageView imgPassword;
    @BindView(R.id.btn_password)
    RelativeLayout btnPassword;
    @BindView(R.id.icon_version)
    ImageView iconVersion;
    @BindView(R.id.icon_system_model)
    ImageView iconSystemModel;
    @BindView(R.id.tv_system_model)
    TextView tvSystemModel;
    @BindView(R.id.rl_system_model)
    RelativeLayout rlSystemModel;
    private LogoutDialog mLogoutDialog;

    @OnClick(R.id.btn_userinfo)
    void btn_userinfo() {
        startActivity( UserInfoActivity.class );
//        startActivity( new Intent( Settings.ACTION_WIFI_SETTINGS ) );
    }

    @OnClick(R.id.btn_password)
    void btn_password() {
        startActivity( PasswordActivity.class );
    }

    @Override
    protected void initViews() {
//        showProgressDialog();
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        getUserInfo();
        tv_version.setText( PackageUtils.getVersionName( this ) );
        if (UtilString.isEmpty( PackageUtils.getDeviceBrand() )) {
            rlSystemModel.setVisibility( View.GONE );
        } else {
            rlSystemModel.setVisibility( View.VISIBLE );
            tvSystemModel.setText( PackageUtils.getDeviceBrand() + " " + PackageUtils.getSystemModel() );
        }
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_user;
    }

    private void getUserInfo() {
        RestProxy.getInstance().getUserInfo( new Observer<UserInfoResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfoResponse userInfoResponse) {
//                dismissProgressDialog();
                tv_name.setText( userInfoResponse.data.realName );
                tv_account.setText( userInfoResponse.data.account );
                tv_school.setText( userInfoResponse.data.orgName );
            }

            @Override
            public void onError(Throwable e) {
                NetUtil.onError( e, UserActivity.this );
            }

            @Override
            public void onComplete() {

            }
        } );
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        showLogoutDialog();
    }

    private void showLogoutDialog() {
//        if (mQualifiedDialog == null) {
        mLogoutDialog = LogoutDialog.newInstance( true );
        mLogoutDialog.show( getSupportFragmentManager(), "mQualifiedDialog" );
        mLogoutDialog.setmListener( new LogoutDialog.OnItemClickListener() {

            @Override
            public void onConfirm() {
                RestClient.getInstance().token = "";
                PreferencesHelper.getInstance().set( UserActivity.this, "token", "" );
                PreferencesHelper.getInstance().set( UserActivity.this, "domain", "" );
                startActivity( LoginActivity.class );
                EventBus.getDefault().postSticky( new Logout( 1 ) );
                finish();
            }

            @Override
            public void onCancel() {
                mLogoutDialog.dismiss();
            }
        } );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        // TODO: add setContentView(...) invocation
        ButterKnife.bind( this );
    }
}
